package com.atguigu.gmall.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.app.BaseAppV1;
import com.atguigu.gmall.realtime.bean.UserRegisterBean;
import com.atguigu.gmall.realtime.util.DateFormatUtil;
import com.atguigu.gmall.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * @Author lzc
 * @Date 2022/5/3 15:27
 */
public class Dws_05_DwsUserUserRegisterWindowApp extends BaseAppV1 {
    public static void main(String[] args) {
        new Dws_05_DwsUserUserRegisterWindowApp()
            .init("Dws_5_DwsUserUserRegisterWindowApp",
                  4005,
                  1,
                  "Dws_5_DwsUserUserRegisterWindowApp",
                  "Dws_5_DwsUserUserRegisterWindowApp",
                  "dwd_user_register"
            );
    }
    
    @Override
    public void handle(StreamExecutionEnvironment env,
                       DataStreamSource<String> stream) {
        /*
          统计个窗口的注册用户数
         */
//        stream.print();
        stream
            .map(JSON::parseObject)
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                    .withTimestampAssigner((obj, ts) -> obj.getLong("ts") * 1000)
            )
            .windowAll(TumblingEventTimeWindows.of(Time.seconds(10)))
            .aggregate(new AggregateFunction<JSONObject, Long, Long>() {
                           @Override
                           public Long createAccumulator() {
                               return 0L;
                           }
                
                           @Override
                           public Long add(JSONObject value, Long accumulator) {
                               return accumulator + 1;
                           }
                
                           @Override
                           public Long getResult(Long accumulator) {
                               return accumulator;
                           }
                
                           @Override
                           public Long merge(Long a, Long b) {
                               return null;
                           }
                       },
                       new ProcessAllWindowFunction<Long, UserRegisterBean, TimeWindow>() {
                           @Override
                           public void process(Context ctx,
                                               Iterable<Long> elements,
                                               Collector<UserRegisterBean> out) throws Exception {
                               String stt = DateFormatUtil.toYmdHms(ctx.window().getStart());
                               String edt = DateFormatUtil.toYmdHms(ctx.window().getEnd());
                    
                               Long result = elements.iterator().next();
                               Long ts = System.currentTimeMillis();
                    
                               out.collect(new UserRegisterBean(stt, edt, result, ts));
                           }
                       }
            )
            .addSink(FlinkSinkUtil.getClickHouseSink("gmall2022", "dws_user_user_register_window", UserRegisterBean.class));
        
    }
}
